package com.centriole.service.workflow.controller;

import com.centriole.service.workflow.entity.Process;
import com.centriole.service.workflow.entity.WorkPage;
import com.centriole.service.workflow.service.ProcessService;
import com.centriole.service.workflow.service.WorkPageService;
import com.centriole.service.workflow.util.AjaxResult;
import com.centriole.service.workflow.util.CommonUtil;
import com.centriole.service.workflow.util.ResultJson;
import com.github.pagehelper.PageInfo;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/flow/workPage")
public class WorkPageController {
	@Resource
	private WorkPageService workPageService;
	@Resource
	private ProcessService processService;
	
	@RequestMapping("/list")
	public String tolist(){
		return "flowpages/workPageList";
	};
	
    @RequestMapping(value = "/edit")
    public String edit(Model model, HttpServletRequest request, HttpServletResponse response, String id) {
    	if(CommonUtil.isNotEmpty(id)){
			model.addAttribute("process", JSONObject.fromObject(workPageService.selectById(id)));
		}else{
			model.addAttribute("process", JSONObject.fromObject(new WorkPage()));
		}
		List<Process> list = processService.select("");
		model.addAttribute("processList", JSONArray.fromObject(list));
        return "flowpages/workPageEdit";
    }
    
    /**
     * 新增工作页面
     *
     * @param model
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public AjaxResult save(WorkPage model, HttpServletRequest request) {
        try {
        	workPageService.insertOrEdit(model);
            return AjaxResult.Success("保存成功", model.getPk());
        } catch (Exception e) {
            return AjaxResult.Error(e.getMessage());
        }
    }
    
    /**
     * 提供json格式数据
     *
     * @param sortName  排序属性
     * @param sortOrder 排序类型 {asc, desc}
     * @param currPage  当前页
     * @param pageSize  页大小
     * @param request   request
     */
    @ResponseBody
    @RequestMapping("/list/json")
    public ResultJson listJson(@RequestParam(value = "sortname", required = false, defaultValue = "") String sortName,
                           @RequestParam(value = "sortorder", required = false, defaultValue = "") String sortOrder,
                           @RequestParam(value = "page", required = false, defaultValue = "") String currPage,
                           @RequestParam(value = "pagesize", required = false, defaultValue = "") String pageSize,
                           HttpServletRequest request) {

		try {
			List<WorkPage> list = workPageService.select();
			//对List进行过滤
			list = list.stream().filter(e->processService.selectByKey(e.getProcessId()) !=null ).collect(Collectors.toList());
			for(WorkPage w:list){
				Process process = processService.selectByKey(w.getProcessId());
				w.setProcessName(process.getProcessName());
			}

			return ResultJson.usePageInfo(new PageInfo<WorkPage>(list));
		} catch (Exception e) {
			return ResultJson.error(ResultJson.INNER_ERR_MSG);
		}
    }
    
    /**
     * 删除工作页面
     * @param ids
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Object delete(@RequestParam(value = "ids", required = false, defaultValue = "") String[] ids, HttpServletRequest request) {
        try {
        	for (String id : ids) {
        		workPageService.logicDelete(id);
            }
            return AjaxResult.Success("删除成功");
        } catch (Exception ce) {
            return AjaxResult.Error(ce.getMessage());
        }
    }
    
	@ResponseBody
	@RequestMapping(value = "/getComboBoxData")
	public Object getComboBoxData(HttpServletRequest request) {
		String processId = request.getParameter("processId");
		String type = request.getParameter("type");
		Process process = processService.selectById(processId);
		return workPageService.selectByProcessIdAndType(process.getProcessKey(), type);
	}
	
	@ResponseBody
	@RequestMapping(value = "/getPageNameById")
	public Object getPageNameById(HttpServletRequest request) {
		 String editPageId = request.getParameter("editPageId");
	        String viewPageId = request.getParameter("viewPageId");
	        Map<String,String> map = new HashMap<String,String>();
	        if(editPageId != null){
	            map.put("editPageName",workPageService.selectById(editPageId).getPageName());
	        }

	        if(viewPageId != null){
	            map.put("viewPageName",workPageService.selectById(viewPageId).getPageName());
	        }
	        return map;
	}
}
